<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入项目类型"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="可用状态状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hissystem:projectFee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hissystem:projectFee:update']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hissystem:projectFee:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postlist" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目费用ID" align="center" prop="projectId"/>
      <el-table-column label="项目名称" align="center" prop="projectName"/>
      <el-table-column label="关键字" align="center" prop="keyword"/>
      <el-table-column label="项目单价" align="center" prop="projectUnitPrice"/>
      <el-table-column label="项目成本" align="center" prop="projectCost"/>
      <el-table-column label="单位(次)" align="center" prop="times"/>
      <el-table-column label="类别" align="center" prop="type"/>
      <el-table-column label="状态" align="center" prop="status":formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hissystem:projectFee:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hissystem:projectFee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目类型" prop="type">
          <el-select
            v-model="form.type"
            placeholder="项目类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="list in projectFeeList"
              :key="list.type"
              :label="list.type"
              :value="list.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item label="项目单价" prop="projectUnitPrice">
          <el-input v-model="form.projectUnitPrice" placeholder="请输入项目单价" />
        </el-form-item>
        <el-form-item label="项目成本" prop="projectCost">
          <el-input v-model="form.projectCost" placeholder="请输入项目成本" />
        </el-form-item>
        <el-form-item label="项目单位" prop="times">
          <el-input v-model="form.times" placeholder="请输入项目单位" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {listProjectFee,selectProjectFeeType,addProjectFee,updateProjectFee,selectProjectFeeById,deleteProjectFees} from "@/api/hissystem/projectFee";

    export default {
        name: "projectFee",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 字典表格数据
                postlist: [],
                // 查询所有数据
                projectFeeList: [],
                //类型字典
                typeOptions: [],
                // 弹出层标题
                title: "",
                // 选中类型
                selected:'',
                // 是否显示弹出层
                open: false,
                // 状态数据字典
                statusOptions: [],
                // 日期范围
                dateRange: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    type: undefined,
                    projectName: undefined,
                    keyword: undefined,
                    projectUnitPrice :undefined,
                    projectCost :undefined,
                    times :undefined,
                    status: undefined
                },
                // 列信息
                columns: [
                    { key: 0, label: `项目费用ID`, visible: true },
                    { key: 1, label: `项目名称`, visible: true },
                    { key: 2, label: `关键字`, visible: true },
                    { key: 3, label: `项目单价`, visible: true },
                    { key: 4, label: `项目成本`, visible: true },
                    { key: 5, label: `项目单位`, visible: true },
                    { key: 6, label: `类别`, visible: true },
                    { key: 7, label: `状态`, visible: true },
                    { key: 8, label: `创建时间`, visible: true }
                ],
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    type: [
                        { required: true, message: "项目类型不能为空", trigger: "blur" }
                    ],
                    projectName: [
                        { required: true, message: "项目名称不能为空", trigger: "blur" }
                    ],
                    projectUnitPrice: [
                        { required: true, message: "项目单价不能为空", trigger: "blur" }
                    ],
                    projectCost: [
                        { required: true, message: "项目成本不能为空", trigger: "blur" }
                    ]
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("hissystem_normal_disable").then(response => {
                this.statusOptions = response.data;
            });
        },
        methods: {
            //  查询项目费用列表
            getList() {
                this.loading = true;
                listProjectFee(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                        this.postlist = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },
            // 状态字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.status);
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    projectId: undefined,
                    projectName: undefined,
                    projectUnitPrice: undefined,
                    projectCost: undefined,
                    type: undefined,
                    status: "0"
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                selectProjectFeeType().then(response => {
                    this.projectFeeList=response.data;
                });
                this.open = true;
                this.title = "新增项目收费";
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.projectId)
                this.single = selection.length!=1
                this.multiple = !selection.length
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                selectProjectFeeType().then(response => {
                    this.projectFeeList=response.data;
                });
                const projectId = row.projectId || this.ids
                selectProjectFeeById(projectId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改检查费用";
                });
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.projectId != undefined) {
                            console.log(this.form);
                            console.log(this.form.params);
                            updateProjectFee(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addProjectFee(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const projectIds = row.projectId || this.ids;
                this.$confirm('是否确认删除项目编号为"' + projectIds + '"的收费项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return deleteProjectFees(projectIds);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
        }
    };
</script>

<style scoped>

</style>
